unit u_corretoritpv;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Rel, RDprint, ExtCtrls, StdCtrls, CheckLst, ComCtrls,
  Buttons, Gauges,db,dbtables;

type
  TfrmCorretor_ITPV = class(TFrmPad_Rel)
    pb: TGauge;
    procedure bimprimirClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function Recupera_Entidade(p_Pedido: String):String;
  end;

var
  frmCorretor_ITPV: TfrmCorretor_ITPV;

implementation

uses Dm, Biblio;

{$R *.DFM}

procedure TfrmCorretor_ITPV.bimprimirClick(Sender: TObject);
var tq,tq1: TQuery;
begin
  inherited;
  tq              := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq1              := Tquery.Create(dmint);
  tq1.databasename := DmInt.Db.DatabaseName;

  tq.active       := False;
  tq.sql.clear;
  tq.sql.add('Select * from ItemPedVenda where ent_Codigo is null');
  tq.Open;
  pb.MinValue:= 0;
  pb.MaxValue:= tq.RecordCount;
  pb.Progress:= 0;
  while not tq.Eof do begin
   pb.Progress:= pb.Progress + 1;
   tq1.active       := False;
   tq1.sql.clear;
   tq1.sql.add('Update ItemPedVenda set Ent_Codigo =:Ent where Ped_Numero =:Ped');
   tq1.ParamByName('Ent').AsString:= Recupera_Entidade(tq.FieldByName('Ped_numero').AsString);
   tq1.ParamByName('Ped').AsString:= tq.FieldByName('Ped_numero').AsString;
   if Recupera_Entidade(tq.FieldByName('Ped_numero').AsString) <> '' then
    tq1.ExecSQL;
   tq.Next;
  end;
  tq.Close;
  tq.Free;
  tq1.Close;
  tq1.Free;
end;

function TfrmCorretor_ITPV.Recupera_Entidade(p_Pedido: String): String;
var tq: TQuery;
begin
  tq              := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active       := False;
  tq.sql.clear;
  tq.sql.add('Select Ent_Codigo from PedidosVenda where Ped_Numero =:Ped');
  tq.ParamByName('Ped').AsString:= p_Pedido;
  tq.Open;
  Result:= '';
  if not tq.Eof then
   Result:= StrZero(tq.FieldByName('Ent_Codigo').AsString,5);
  tq.Close;
  tq.Free;
end;

end.
